package com.senven.api.service;

import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.senven.api.commom.ResponsePage;
import com.senven.api.dao.DepartmentMapper;
import com.senven.api.pojo.Department;
import com.senven.api.pojo.Organ;
import org.apache.ibatis.annotations.Param;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author LQP
 */
@Component
@Service

public class DepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;


    public List<Department> queryDepartmentList(){
        return departmentMapper.selectList(null);
    }

    public Department queryDepartmentById(int did,int oid){
        return departmentMapper.queryDepartmentListById(did,oid);
    }


    public int deleteDepartment(int oid,int did){
        return departmentMapper.deleteDepartment(oid, did);
    }

    public int insertDepartment(Department department){
        return departmentMapper.insertDepartment(department);
    }

    public int updateDepartment(Department department){
        return departmentMapper.updateDepartment(department);
    }

    /**
     * 查询该社团所有部门
     * @param current 当前页数
     * @param size 每页记录大小
     * @param oid 社团id
     * @return
     */
    public ResponsePage<Department> getDepartmentList(int oid,int current, int size){
        Page<Department> page = new Page<Department>(current, size);
        QueryWrapper<Department> wrapper = new QueryWrapper<Department>();
        wrapper.eq("oid",oid);

        IPage<Department> iPage = departmentMapper.selectPage(page,null);

        ResponsePage<Department> responsePage= new ResponsePage<Department>();
        responsePage.makePage(iPage);

        return responsePage;
    }

    /**
     * 名称模糊查询社团部门
     * @param oid
     * @param name
     * @param current
     * @param size
     * @return
     */
    public ResponsePage<Department> queryDepartmentByName(int oid, String name, int current, int size){
        Page<Department> page = new Page<Department>(current, size);
        QueryWrapper<Department> wrapper = new QueryWrapper<Department>();
        wrapper.like("name",name).eq("oid",oid);

        IPage<Department> iPage = departmentMapper.selectPage(page, wrapper);

        //自定义页数对象
        ResponsePage<Department> responsePage= new ResponsePage<Department>();
        responsePage.makePage(iPage);

        return responsePage;
    }

}
